<?php
/**
 *
 * @author yusaint
 *        
 */
class Pfinal_Model_Adapter_RMDBS implements Pfinal_Model_Adapter_Interface {
	protected $parser;
	
	protected $socket;
	/**
	 *
	 * @param IProtocolParser $parser
	 */
	public function __construct(Pfinal_Model_Protocol_IParser $parser=null){
		if(is_null($parser)){
			$parser=new Pfinal_Model_Protocol_RMDBSParser();
		}
		$this->parser=$parser;
	}
	
	/**
	 * 初始化链接
	 * @param unknown_type $params
	 */
	public function init($params){
		$this->socket = mysql_connect($params['host'].':'.$params['port'],$params['user'],$params['password']);
		if (!$this->socket){
			throw new Pfinal_Exception_Runtime("can not connect to mysql");
		}
		mysql_select_db($params['database'],$this->socket);
	}
	/*
	 * (non-PHPdoc) @see Pfinal_Model_Adapter_Interface::query()
	 */
	public function query(Pfinal_Model_Selector $selector) {
		// TODO Auto-generated method stub
		$sql = $this->parser->parse($selector);
		print_r($sql);
		$result = mysql_query($sql,$this->socket);
		$rets = array();
		if ($result){
			while (($row = mysql_fetch_assoc($result))!=false){
				$rets[] = $row;
			}
		}
		return $rets;
	}
	
	/**
	 * (non-PHPdoc)
	 * @see Pfinal_Model_Adapter_Interface::insert()
	 */
	public function insert(Pfinal_Model_Insert $insertor){
		
	}
	
	/**
	 * (non-PHPdoc)
	 * @see Pfinal_Model_Adapter_Interface::execute()
	 */
	public function execute(Pfinal_Model_Update $updater){
		
	}
	
	public function call(Pfinal_Model_DDL $ddl){
		$sql = $this->parser->parse($ddl);
		$result = mysql_query($sql,$this->socket);
		$rets = array();
		if ($result){
			while (($row = mysql_fetch_assoc($result))!=false){
				$rets[] = $row;
			}
		}
		return $rets;
	}
}

?>